package com.onlyphoto.onlymall.mvp.view.fragment

import android.content.Context
import android.content.Intent
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseFragment
import com.onlyphoto.onlymall.mvp.contract.CLoginByPwd
import com.onlyphoto.onlymall.mvp.presenter.PLoginByPwdImpl

import android.os.Bundle
import android.text.*
import android.view.View
import com.onlyphoto.onlymall.bean.request.LoginBean
import com.onlyphoto.onlymall.bean.response.UserInfoBean
import com.onlyphoto.onlymall.interfaces.PhoneCallback
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.mvp.view.activity.ForgetPwdActivity
import com.onlyphoto.onlymall.mvp.view.activity.LoginActivity
import com.onlyphoto.onlymall.mvp.view.activity.ResetPwdActivity
import com.onlyphoto.onlymall.utils.*
import kotlinx.android.synthetic.main.fragment_loginbypwd.*
import kotlinx.android.synthetic.main.fragment_loginbypwd.et_phone_number
import kotlinx.android.synthetic.main.fragment_loginbypwd.login


class LoginByPwdFragment : BaseFragment<PLoginByPwdImpl>(), CLoginByPwd.IVLoginByPwd {

    private var pwdIsVisible = false
    private var phone = ""
    private var callback: PhoneCallback? = null

    override fun getLoginEntity(): LoginBean {
        val loginEntity = LoginBean()
        loginEntity.post_type = 2
        loginEntity.phone = et_phone_number.text.toString().trim()
        loginEntity.password = et_password.text.toString().trim()
        return loginEntity
    }

    override fun retry() {}

    override fun init() {
        phone = arguments!!.getString("phone", "")

        et_phone_number.setText(phone)
        initListener()
    }

    private fun initListener() {
        AndroidUtils.isButtonEnable(intArrayOf(11, 6), login, et_phone_number, et_password)

        val listener = object : NoDoubleClickListener() {
            override fun onNoDoubleClick(view: View?) {
                phone = et_phone_number.text.toString().trim()
                when (view?.id) {
                    R.id.login ->
                        mPresenter!!.loginByPwd()
                    R.id.pwd_visibility -> {
                            if (pwdIsVisible) {
                                pwd_visibility.setImageResource(R.mipmap.login_eye_close)
                                et_password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                            } else {
                                pwd_visibility.setImageResource(R.mipmap.login_eye_open)
                                et_password.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                            }
                            pwdIsVisible = !pwdIsVisible
                            val editAble = et_password.text
                            Selection.setSelection(editAble, editAble.length)
                        }
                    R.id.forget_pwd -> {
                        //验证phone的合法性
                        if (!StringUtil.isMobileNO(phone)) {
                            showMsg("请输入正确的手机号码")
                            return
                        }
                        startActivity(Intent(activity, ResetPwdActivity::class.java).putExtra("phone", phone))
                    }
                }
            }
        }

        login.setOnClickListener(listener)
        pwd_visibility.setOnClickListener(listener)
        forget_pwd.setOnClickListener(listener)
    }

    override fun createPresenter() {
        mPresenter = PLoginByPwdImpl(mContext, this)
    }

    override fun showMsg(msg: String) {
        ToastUtil.showToast(msg)
    }

    override fun verifyPhoneSuccess(isReal: Boolean) {
        val intent = Intent(context, ForgetPwdActivity::class.java)
        intent.putExtra("phone", phone)
        intent.putExtra("isReal", isReal)
        startActivity(intent)
    }

    override fun loginSuccess(bean: UserInfoBean) {
        UserUtil.saveUserInfo(activity, bean)
        SharePreferenceUtil.getInstance(activity).setBoolData(Constants.IS_PHONE_LOGIN, true)
        //保存号码
        SharePreferenceUtil.getInstance(activity).setData(Constants.SHARE_TAG_LOGIN_PHONE, phone)
        activity?.finish()
    }

    override fun showLoading() {
        defaultPostLoadingDialog?.isCancelable = false
        showDefaultDialog()
    }

    override fun hideLoading() {
        hideDefaultDialog()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        callback = context as PhoneCallback

    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            et_phone_number?.setText(LoginActivity.phoneNum)
            et_phone_number?.setSelection(LoginActivity.phoneNum!!.length)
        } else {
            val phone = et_phone_number?.text.toString().trim()
            callback?.phoneCange(phone)
        }
    }

    override fun getLayoutRes(): Int {
        return R.layout.fragment_loginbypwd
    }

    companion object {

        fun newInstance(): LoginByPwdFragment {
            val fragment = LoginByPwdFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

}
